/**
 * LocationAPITools library, version 1.0.2
 * 
 * WWW: http://code.google.com/p/locationapitools
 *   
 * Summary:
 *   A collection of classes that simplify the development of location-aware mobile
 *   applications using the JSR-179 Location API on the J2ME (CLDC/MIDP) platform,
 *   e.g. on GPS-equipped smartphones.
 * 
 * Author:
 *   Matthias Stevens (Sony CSL Paris / Vrije Universiteit Brussel)
 *   Contact: matthias.stevens@gmail.com
 *  
 * Description:
 *   The primary use case is a location tracker that stores tracks in KML
 *   (cfr. Google Earth/Maps), CSV or plain text format.
 * 
 *   Package "com.sony.csl.j2me.locationapitools.demo" contains a MIDlet class that
 *   demonstrates how LocationAPITools can be used to create simple GPS tracker
 *   application that produces KML files. For examples of more advanced uses of this
 *   library refer to the NoiseTube project (see below, look for "NoiseTube MobileNoiseTagger").
 * 
 *   The code is known to be working on the following Symbian OS-based platforms:
 *     * S60 3rd Edition Feature Pack 1 (Symbian OS 9.2), e.g.: Nokia N95 and Nokia N95 8GB smartphones
 *     * S60 3rd Edition Feature Pack 2 (Symbian OS 9.3) 
 *   Although this is untested, the code should also work on other (non-Symbian/S60)
 *   J2ME CLDC/MIDP platforms/devices, as long as the JSR-179 Location API is supported
 *   on the platform/device in question.
 * 
 *   This library was developed within the scope of the NoiseTube project at
 *   Sony Computer Science Laboratory (CSL) Paris, for more information please refer to:
 *     * http://noisetube.net
 *     * http://code.google.com/p/noisetube
 *     * http://www.csl.sony.fr 
 *    
 * License: 
 *   Copyright 2008-2009 Sony CSL Paris
 *   Licensed under the Apache License, Version 2.0 (the "License"); you may not use
 *   this file except in compliance with the License. You may obtain a copy of the
 *   License at http://www.apache.org/licenses/LICENSE-2.0.
 *   Unless required by applicable law or agreed to in writing, software distributed
 *   under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 *   CONDITIONS OF ANY KIND, either express or implied. See the License for the
 *   specific language governing permissions and limitations under the License.
 */

package com.sony.csl.j2me.locationapitools.demo;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

import com.sony.csl.j2me.io.FileAccess;


/**
 * @author mstevens
 * 
 */
public class NewTrackForm extends Form implements CommandListener
{
	
    //MIDlet:
    private DemoMIDlet midlet;

	//track formats:
	static public final int KML_FORMAT = 1;
	static public final int CSV_FORMAT = 2;
	static public final int TXT_FORMAT = 3;

	//GUI elements:
	private Command cmdStart;
	private Command cmdCancel;
	private TextField txtPath;
	private TextField txtTrackName;
	private ChoiceGroup chgrFormat;

	private int idxKML;
	private int idxCSV;
	private int idxTXT;

	
	public NewTrackForm(DemoMIDlet midlet)
	{
		super("New track...");
		this.midlet = midlet;
		
		//Widgets:
		txtPath = new TextField("Path to folder to store track:", DemoMIDlet.DEFAULT_PATH, 256, TextField.ANY);
		this.append(txtPath);
		txtTrackName = new TextField("Track name:", DemoMIDlet.DEFAULT_TRACKNAME, 75, TextField.ANY);
		this.append(txtTrackName);
		chgrFormat = new ChoiceGroup("Track format:", Choice.EXCLUSIVE);
		idxKML = chgrFormat.append("KML", null);
		idxCSV = chgrFormat.append("CSV", null);
		idxTXT = chgrFormat.append("TXT", null);
		chgrFormat.setSelectedIndex(idxKML, true); //kml is the default format
		this.append(chgrFormat);

		//Commands:
		cmdStart = new Command("Start!", Command.OK, 1);
		cmdCancel = new Command("Cancel", Command.CANCEL, 1);
		this.addCommand(cmdStart);
		this.addCommand(cmdCancel);
		
		this.setCommandListener(this);
	}

	/**
	 * @return the chosen folder path
	 */	
	public String getPath()
	{
		return txtPath.getString();
	}
	
	/**
	 * @return the chosen track name
	 */
	public String getTrackName()
	{
		return txtTrackName.getString();
	}
	
	/**
	 * @return the chosen track format
	 */
	public int getTrackFormat()
	{
		if(chgrFormat.getSelectedIndex() == idxKML)
			return KML_FORMAT;
		if(chgrFormat.getSelectedIndex() == idxCSV)
			return CSV_FORMAT;
		if(chgrFormat.getSelectedIndex() == idxTXT)
			return TXT_FORMAT;
		return KML_FORMAT;  //this should not happen, but KML is the default format anyway
	}
	
	public void commandAction(Command cmd, Displayable disp)
	{
		if(cmd == cmdStart)
		{
			if(FileAccess.isValidFileName(getTrackName()))
			{
				DemoMIDlet.getDisplay().setCurrent(midlet.getMainForm());
				midlet.startTracking();
			}
			else
			{	//invalid track name, show alert and "stay here" (return true to indicate that command was handled by the form)
				Alert errorAlert = new Alert("LocationAPITools Demo", "Trackname contains invalid characters (*, <, >, :, \", \\, / or |)", null, AlertType.ERROR);
				DemoMIDlet.getDisplay().setCurrent(errorAlert, this);
			}
		}
		if(cmd == cmdCancel)
		{
			DemoMIDlet.getDisplay().setCurrent(midlet.getMainForm());
		}
	}

}
